var $baidu = {
	packages : {
		"distance" : [ "DistanceTool_min.js" ],
		"draw" : [ "DrawingManager_min.js", "DrawingManager_min.css" ],
		"marker" : [ "MarkerTool_min.js" ],
		"zoom" : [ "RectangleZoom_min.js" ],
		"window" : [ "SearchInfoWindow.js", "SearchInfoWindow_min.css" ],
	},
	install : function(pks) {
		for (var i in pks) {
			var file = this.packages[pks[i]];
			if (file) {
				for (var i = 0; i < file.length; i++) {
					var f = file[i];
					if (f.indexOf(".css") == -1) {
						var script = document.createElement("script");
						script.setAttribute("type", "text/javascript");
						script.setAttribute("src", BMAP.webcontent + f);
						document.body.appendChild(script);
					} else {
						var link = document.createElement("link");
						link.setAttribute("rel", "stylesheet");
						link.setAttribute("type", "text/css");
						link.setAttribute("href", BMAP.webcontent + "../css/" + f);
						document.body.appendChild(link);
					}
				}
			}
		}
	},
	currMap : null,
	maps : {},
	createMap : function(json) {
		var baiduMap = function(tid, idx) {
			idx = idx | 0;
			this.id = idx;
			this.ol = null;
			this.map = new BMap.Map(tid, {
			});
			this.idx = idx;
		};
		var bm = new baiduMap(json["id"], json["idx"]);
		var point = new BMap.Point(111.404, 40.915);
		bm.map.centerAndZoom(point, 5);
		if (json["minzoom"])
			bm.map.setMinZoom(json.minzoom);
		if (json["maxzoom"])
			bm.map.setMaxZoom(json.maxzoom);
		/*BMAP_NORMAL_MAP*/
		bm.map.setMapType(BMAP_NORMAL_MAP)
		bm.map.enableScrollWheelZoom();
		bm.map.addControl(new BMap.NavigationControl());
		bm.map.enableScrollWheelZoom();
		bm.map.enableKeyboard();
		bm.map.enableAutoResize();

		bm.map.addEventListener("tilesloaded", function() {});
		bm.map.addEventListener("zoomend", function() {});
		bm.map.addEventListener("moveend", function() {});
		try {
			maps[bm.map.idx] = bm;
			this.currMap = bm;
		} catch (e) {}
		return bm;
	},
	infoWindow : null,
	openWindow : function(bm, json, e) {
		if ($baidu.infoWindow) {
			$baidu.infoWindow.close();
		}
		$baidu.infoWindow = new BMapLib.SearchInfoWindow(bm.map,
			json.content, {
				title : json.title,
				width : json.width | 320,
				enableSendToPhone : false,
				searchTypes : [],
				enableAutoPan : false
			});
		$baidu.infoWindow.open(new BMap.Point(e.point.lng, e.point.lat));
	},
	lastControl : null,
	createDistance : function(bm) {
		this.closeControl();
		var toolDis = new BMapLib.DistanceTool(bm.map);
		this.lastControl = toolDis;
		toolDis.open();
		return toolDis;
	},
	createZoomOut : function(bm) {
		this.closeControl();
		var reczoomInOut = new BMapLib.RectangleZoom(bm.map, {
			followText : "\u62D6\u62FD\u9F20\u6807\u8FDB\u884C\u64CD\u4F5C",
			autoClose : true
		});
		this.lastControl = reczoomInOut;
		reczoomInOut.open();
		return reczoomInOut;
	},
	createDrawing : function(bm) {
		this.closeControl();
		var map = bm.map;
		var styleOptions = {
			strokeColor : "red",
			fillColor : "red",
			strokeWeight : 1,
			strokeOpacity : 0.8,
			fillOpacity : 0.6,
			strokeStyle : 'solid'
		};
		BMapLib.DrawingManager.prototype.close = function() {
			bm.drawingManager.close();
			bm.drawingManager.removeEventListener('overlaycomplete');
			bm.drawingManager = null;
		};
		bm.drawingManager = new BMapLib.DrawingManager(map, {
			isOpen : false,
			enableDrawingTool : true,
			drawingToolOptions : {
				anchor : BMAP_ANCHOR_TOP_RIGHT,
				offset : new BMap.Size(-78, 25),
				scale : 0.5,
				drawingModes : [ BMAP_DRAWING_RECTANGLE, BMAP_DRAWING_CIRCLE,
					BMAP_DRAWING_POLYLINE, BMAP_DRAWING_POLYGON ]
			},
			rectangleOptions : styleOptions,
			circleOptions : styleOptions,
			polylineOptions : styleOptions,
			polygonOptions : styleOptions
		});
		var overlaycomplete = function(e) {
			var overlay = e.overlay;
			overlay.drawingMode = e.drawingMode;
			overlay.type = "draw";
		};
		bm.drawingManager.addEventListener('overlaycomplete', overlaycomplete);
		this.lastControl = drawingManager;
		drawingManager.open();
		return bm.drawingManager;
	},
	closeControl : function() {
		if (this.lastControl) {
			this.lastControl.close();
		}
	}
};